// 认证相关的工具函数

/**
 * 设置用户登录状态
 */
export function setUserLogin(user) {
  localStorage.setItem('userId', user.uid);
  localStorage.setItem('username', user.username);
  localStorage.setItem('isLoggedIn', 'true');
}

/**
 * 获取当前登录用户ID
 */
export function getCurrentUserId() {
  return localStorage.getItem('userId');
}

/**
 * 获取当前登录用户名
 */
export function getCurrentUsername() {
  return localStorage.getItem('username');
}

/**
 * 检查用户是否已登录
 */
export function isLoggedIn() {
  return localStorage.getItem('isLoggedIn') === 'true';
}

/**
 * 用户登出
 */
export function logout() {
  localStorage.removeItem('userId');
  localStorage.removeItem('username');
  localStorage.removeItem('isLoggedIn');
  // 清除旧的用户数据
  localStorage.removeItem('currentUser');
}

/**
 * 调用后端登出API
 */
export async function logoutFromServer() {
  try {
    const response = await fetch('http://localhost:2626/api/user/logout', {
      method: 'POST',
      credentials: 'include'
    });
    const result = await response.json();
    return result.success;
  } catch (error) {
    console.error('登出API调用失败:', error);
    return false;
  }
} 